package component

import (
	"github.com/natefinch/lumberjack"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"os"
	"time"
)

func InitLogger() *zap.SugaredLogger {
	var coreArr []zapcore.Core
	lumber := &lumberjack.Logger{
		Filename:   "./log/log.log", //日志文件的位置
		MaxSize:    10,              //在进行切割之前，日志文件的最大大小（以MB为单位）
		MaxBackups: 5,               //保留旧文件的最大个数
		MaxAge:     30,              //保留旧文件的最大天数
		Compress:   false,           //是否压缩/归档旧文件
	}
	write := zapcore.AddSync(lumber)
	encoderConfig := zap.NewProductionEncoderConfig()
	encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
	encoderConfig.EncodeLevel = zapcore.CapitalLevelEncoder
	encoder := zapcore.NewConsoleEncoder(encoderConfig)
	//同时在文件与控制台输出
	core := zapcore.NewCore(encoder, zapcore.NewMultiWriteSyncer(write, zapcore.AddSync(os.Stdout)), zapcore.DebugLevel)
	coreArr = append(coreArr, core)
	logger := zap.New(zapcore.NewTee(coreArr...), zap.AddCaller())
	Log := logger.Sugar()
	defer Log.Sync()
	Log.Info("-----------------------------server start on " + time.Now().Format("2006-01-02 15:04:05") + "-----------------------------")
	return Log
}
